<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css">
</head>
<style>
</style>
<body id="body" style="padding-bottom: 90px; background-color: #ECF0F5;">
	<div class="modal-content">
		<div class="modal-body">
			<div class="input-group">
				<span class="input-group-addon">标题：</span> <input type="text" class="form-control" id="name">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">服务类型：</span> <select class="form-control" id="typeId">

				</select>
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">价格：</span> <input type="text" class="form-control" id="price" oninput="this.value=this.value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">联系电话：</span> <input type="text" class="form-control" id="tel">
			</div>
			<br />
			<div class="form-group">
				<label for="mien_pic" class="control-label">物品图片(238*178)：</label>
				<button id="imgBtn0" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile0.click();">上传</button>
				<img id="image0" class="img-rounded" src="" width="150px;" style="cursor: pointer;" onclick="imgFile0.click();" /> <input type="file" class="form-control" style="display: none;" name="imgFile" id="imgFile0" onchange="fileChange()">
			</div>
			<div class="form-group">
				<label for="mien_pic" class="control-label">活动内容：</label>
				<textarea class="form-control" style="width: 100%; height: 500px;" id="column1_textarea" name="kindEditor_content"></textarea>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>

<!-- kindeditor 编辑器 -->
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/kindeditor.js"></script>
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script>
	var lock = false;
	var id = "${map['id']}";
	var index = parent.layer.getFrameIndex(window.name);
	var imgUrl;
	var editor;
	var options = {
		items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print',
				'template', 'code', 'cut', 'copy', 'paste', 'plainpaste',
				'wordpaste', '|', 'justifyleft', 'justifycenter',
				'justifyright', 'justifyfull', 'insertorderedlist',
				'insertunorderedlist', 'indent', 'outdent', 'subscript',
				'superscript', 'clearhtml', 'quickformat', 'selectall', '|',
				'fullscreen', '/', 'formatblock', 'fontname', 'fontsize', '|',
				'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				'strikethrough', 'lineheight', 'removeformat', '|', 'image',
				'multiimage', 'flash', 'media', 'insertfile', 'table', 'hr',
				'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link',
				'unlink', '|', 'about' ], //配置kindeditor编辑器的工具栏菜单项
		cssPath : 'kindeditor-4.1.10/plugins/code/prettify.css',
		allowFileManager : true,
		filterMode : true, //是否启用过滤模式(编辑文本框中是否可以出现控件显示)
		htmlTags : {
			span : [ '.color', '.background-color', '.font-size',
					'.font-family', '.background', '.font-weight',
					'.font-style', '.text-decoration', '.vertical-align',
					'.line-height' ],
			font : [ 'id', 'class', 'color', 'size', 'face',
					'.background-color' ],
			a : [ 'id', 'class', 'href', 'target', 'name' ],
			embed : [ 'id', 'class', 'src', 'width', 'height', 'type', 'loop',
					'autostart', 'quality', '.width', '.height', 'align',
					'allowscriptaccess' ],
			img : [ 'id', 'class', 'src', 'width', 'height', 'border', 'alt',
					'title', 'align', '.width', '.height', '.border' ],
			'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [ 'id', 'class',
					'align', '.text-align', '.color', '.background-color',
					'.font-size', '.font-family', '.background',
					'.font-weight', '.font-style', '.text-decoration',
					'.vertical-align', '.text-indent', '.margin-left' ],
			pre : [ 'id', 'class' ],
			hr : [ 'id', 'class', '.page-break-after' ],
			'br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del' : [ 'id',
					'class' ],
			iframe : [ 'id', 'class', 'src', 'frameborder', 'width', 'height',
					'.width', '.height' ]
		},
		allowFileManager : true,
		allowUpload : true, //允许上传图片
		imageUploadJson : '/saveImg', //服务端上传图片处理URI
		uploadJson : '${pageContext.request.contextPath}/upload_img', //上传
		// 		fileManagerJson : '${pageContext.request.contextPath}/mien_file_manager.do', // 文件管理
		resizeType : 0,//使其不可以拖动 
		afterCreate : function() {
		},
		afterBlur : function() {
			this.sync();
		}
	}
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="kindEditor_content"]', options);
	});

	$(function() {
		$
				.ajax({
					url : "${pageContext.request.contextPath}/selectPropertyServiceTypeByAll",
					type : "post",
					dataType : "json",
					success : function(map) {
						var list = map['list'];
						var options = "<option value=''>请选择...</option>";
						for (var i = 0; i < list.length; i++) {
							options += "<option value='"+list[i].id+"'>"
									+ list[i].name + "</option>";
						}
						$("#typeId").html(options);
						if (id != '') {
							$
									.ajax({
										url : "${pageContext.request.contextPath}/selectPropertyServiceById",
										type : "post",
										data : {
											id : id
										},
										dataType : "json",
										success : function(map) {
											var goods = map['goods'];
											$("#name").val(goods.name);
											$("#price").val(goods.price);
											$("#tel").val(goods.tel);
											$("#typeId").val(goods.typeId);
											imgUrl = goods.imgUrl;
											if (imgUrl != null & imgUrl != '') {
												$('#image0').attr(
														'src',
														'${pageContext.request.contextPath}/'
																+ imgUrl);
												$('#image0')
														.attr('style',
																'width: 238px; height: 178px;');
												$("#image0").show();
												$("#imgBtn0").hide();
											}
											editor.html(activity.content);
											$("#column1_textarea").val(
													activity.content);
										}
									});
						}
					}
				});
	});

	//添加或修改
	function insertExplodingActivity() {
		lock = true;
		var name = $("#name").val();
		var price = $("#price").val();
		var tel = $("#tel").val();
		var typeId = $("#typeId").val();
		var column1_textarea = $("#column1_textarea").val();
		var phone = /^1\d{10}$/;
		if (name === '' || price === '' || tel === '' || typeId === ''
				|| imgUrl === '' || column1_textarea === '') {
			layer.alert("请完整填写数据！");
			lock = false;
			return;
		}
		if (phone.test(tel) == false) {
			layer.alert("手机输入不合法!");
			that.lock = false;
			return;
		}
		$.ajax({
			url : '${pageContext.request.contextPath}/insertPropertyService',
			type : "post",
			data : {
				id : id,
				name : name,
				imgUrl : imgUrl,
				tel : tel,
				typeId : typeId,
				price : price,
				detail : column1_textarea
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						parent.query();
						parent.layer.close(index);
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
					lock = false;
				}
			}
		});
	}

	function fileChange() {
		$.ajaxFileUpload({
			fileElementId : 'imgFile0', //需要上传的文件域的ID，即<input type="file">的ID。
			url : "${pageContext.request.contextPath}/upload_img", //后台方法的路径
			type : 'post', //当要提交自定义参数时，这个参数要设置成post
			dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
			secureuri : false, //是否启用安全提交，默认为false。
			async : true, //是否是异步
			success : function(data) { //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
				imgUrl = data.url;
				$('#image0').attr('src',
						'${pageContext.request.contextPath}/' + data.url);
				$('#image0').attr('style', 'width: 238px; height: 178px;');
				$("#image0").show();
				$("#imgBtn0").hide();
			},
			error : function(data, status, e) { //提交失败自动执行的处理函数。
				console.error(e);
			}
		});
	}

	function image_check(feid) { //自己添加的文件后缀名的验证
		var img = document.getElementById(feid);
		return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function() {
			layer.alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
			return false;
		})();
	}

	function insert() {
		if (!lock) {
			insertExplodingActivity();
		}
	}
</script>
</html>
